package com.zhuizikejithreeig.cn.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhuizikejithreeig.cn.content.ContectUtils;
import com.zhuizikejithreeig.cn.threeigkeji.CompontApplayCation;
import com.zhuizikejithreeig.cn.threeigkeji.MainActivity;
import com.zhuizikejithreeig.cn.threeigkeji.R;
import com.zhuizikejithreeig.cn.threeigkeji.loginfo.BundingSetActivity;
import com.zhuizikejithreeig.cn.threeigkeji.loginfo.ChoceOtherActivity;
import com.zhuizikejithreeig.cn.threeigkeji.loginfo.LoginActivity;
import com.zhuizikejithreeig.cn.threeigkeji.loginfo.SaoMaActivity;
import com.zhuizikejithreeig.cn.utils.Utils;


public class SimpleCardFragment extends Fragment {
    private String mTitle;
    private ImageView mImageView;
    private TextView mTextButton;
    private LinearLayout mLinearLayout;
    private String uri;
    private String mac;
    private Button mProgressBar;
    private TextView handtext;
    private TextView foottext;
    private String mPhone;
    private boolean isStart=true;

    //这是单例模式,懒汉式
    public static SimpleCardFragment getInstance(String title, String uri, String mac) {
        SimpleCardFragment sf = new SimpleCardFragment();
        sf.mTitle = title;
        sf.uri = uri;
        sf.mac = mac;
        return sf;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fr_simple_card, null);
        mImageView = (ImageView) v.findViewById(R.id.image);
        mTextButton = (TextView) v.findViewById(R.id.text_button);
        handtext = (TextView) v.findViewById(R.id.handtext);
        foottext = (TextView) v.findViewById(R.id.foottext);
        mLinearLayout = (LinearLayout) v.findViewById(R.id.linelayout);
        mProgressBar = (Button) v.findViewById(R.id.progressview);
        mPhone=Utils.getString(ContectUtils.PHONESAVE);

        mProgressBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mTitle未绑定的第几个座椅
                if (TextUtils.isEmpty(mPhone)&&!Utils.isPhoneNO(mPhone)) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), LoginActivity.class);
                    startActivity(intent);
                } else {

                    //当状态为4的时候,说明已经绑定过了
                    if (TextUtils.equals("4",Utils.getString(ContectUtils.STATUS))) {
                        Intent intent = new Intent();
                        intent.setClass(getContext(), SaoMaActivity.class);
                        startActivity(intent);
                    }else {
                        //当状态不为4的时候,说明没有经绑定过了
                        Intent intent = new Intent();
                        intent.setClass(getContext(), BundingSetActivity.class);
                        startActivity(intent);

                    }
                }
            }
        });
        if (TextUtils.equals(mTitle, "0")) {
            handtext.setVisibility(View.INVISIBLE);
            foottext.setVisibility(View.INVISIBLE);
            if (TextUtils.isEmpty(uri)) {

                mProgressBar.setVisibility(View.VISIBLE);
                mImageView.setVisibility(View.GONE);

            } else {
                mImageView.setVisibility(View.GONE);
                mLinearLayout.setVisibility(View.VISIBLE);

                if (isStart){
                    mTextButton.setText("开始按摩");
                    mTextButton.setSelected(true);
                    mTextButton.setTextColor(getContext().getResources().getColor(R.color.textColor3));
                }else {
                    mTextButton.setText("结束按摩");
                    mTextButton.setSelected(false);
                    mTextButton.setTextColor(getContext().getResources().getColor(R.color.textColor4));
                }

                mTextButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (TextUtils.equals(mTextButton.getText().toString().trim(), "开始按摩")) {
                            mTextButton.setText("结束按摩");
                            mTextButton.setSelected(false);
                            mTextButton.setTextColor(getContext().getResources().getColor(R.color.textColor4));
                            isStart = false;
                        } else {
                            isStart = true;
                            mTextButton.setText("开始按摩");
                            mTextButton.setSelected(true);
                            mTextButton.setTextColor(getContext().getResources().getColor(R.color.textColor3));
                        }
                    }
                });

            }
        } else {
            if (TextUtils.isEmpty(uri)) {
                mProgressBar.setVisibility(View.VISIBLE);
                mImageView.setVisibility(View.GONE);
                handtext.setVisibility(View.INVISIBLE);
                foottext.setVisibility(View.INVISIBLE);
            } else {
                handtext.setVisibility(View.VISIBLE);
                foottext.setVisibility(View.VISIBLE);
                mProgressBar.setVisibility(View.GONE);
                mImageView.setVisibility(View.VISIBLE);
                System.out.println("uri=====" + uri);
                Glide.with(getActivity()).
                        load(uri).
                        asBitmap().
                        placeholder(R.mipmap.laoding).//加载中显示的图片
//                                error(R.mipmap.eeror).//加载失败时显示的图片
        into(mImageView);
            }

        }


        return v;
    }
}